/*
 * comlan
 */

package com.promfy.service.log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.promfy.service.log.LogService.ProcessInfo;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.TrafficStats;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

public class NetFlowService extends Service {
	private static final String TAG = "mt_nf";
	private static final String ACTION_RECORD_NET_FLOW = "com.comlan.mterm.action.netflow";
	private static final long INTERVAL_TIME = 1000 * 60 * 60; //one hour
	private NetFlowReceiver netFlowReceiver;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		register();
		deploySwitchLogFileTask();
		Log.d(TAG, "NetFlowService onCreate()");
		new InserToDBThread().start();
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		Log.d(TAG, "NetFlowService onStart()");
	}

	private void register() {
		IntentFilter netFlowFilter = new IntentFilter();
		netFlowFilter.addAction(ACTION_RECORD_NET_FLOW);
		netFlowReceiver = new NetFlowReceiver();
		registerReceiver(netFlowReceiver, netFlowFilter);
	}

	private void deploySwitchLogFileTask() {
		Intent intent = new Intent(ACTION_RECORD_NET_FLOW);
		PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 0);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
		am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), INTERVAL_TIME, sender);
	}
	
	class NetFlowReceiver extends BroadcastReceiver {
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (ACTION_RECORD_NET_FLOW.equals(action)) {
				new InserToDBThread().start();
			} 
		}
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(netFlowReceiver);
		Log.d(TAG, "NetFlowService onDestroy()");
	}
	
    class InserToDBThread extends Thread {
        
        public InserToDBThread(){
                super("InserToDBThread");
                Log.d(TAG, "InserToDBThread is create");
        }
        
        @Override
        public void run() {
               
        }
}
	
	
}
